//
//  FTBaseWebViewController.m
//  FashionTech
//
//  Created by JackieYip on 16/8/23.
//  Copyright © 2016年 mondial-d. All rights reserved.
//

#import "FTBaseWebViewController.h"
#import "FTInternationalization_Macro.h"
#import "FTMacro_Font.h"

@interface FTBaseWebViewController () 
@property (strong, nonatomic, readwrite) UIWebView *webView;
@property (strong, nonatomic, readwrite) UILabel *loadingTips;
@end

@implementation FTBaseWebViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupWebView];
}

- (void)setupWebView
{
    CGRect rect = self.view.bounds;
    rect.origin.y += 64;
    rect.size.height -= 64;
    self.webView = [[UIWebView alloc] initWithFrame:rect];
    self.webView.delegate = self;
    if (_url) {
    [self.webView loadRequest:[NSURLRequest requestWithURL:_url]];
    }
    else if (_HTMLString.length > 0) {
        [self.webView loadHTMLString:_HTMLString baseURL:nil];
    }
    self.webView.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request nawww:(UIWebViewNavigationType)navigationType
{
    // tips
    if (![self.view.subviews containsObject:self.loadingTips])
        [self.view addSubview:self.loadingTips];
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.loadingTips removeFromSuperview];
    if (self.navigationItem.title.length == 0)
        self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - getter
- (UILabel *)loadingTips {
    if (!_loadingTips) {
        self.loadingTips = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 80)];
        self.loadingTips.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.8];
        self.loadingTips.textColor = [UIColor whiteColor];
        self.loadingTips.center = self.view.center;
        self.loadingTips.layer.cornerRadius = 5.0;
        self.loadingTips.layer.masksToBounds = YES;
        self.loadingTips.textAlignment = NSTextAlignmentCenter;
        self.loadingTips.font = kFont16;
        self.loadingTips.text = FTLocalizedStringInLocalizable(@"加载中......");
    }
    return _loadingTips;
}
@end
